package net.wanho.index.controller;

import com.alibaba.fastjson.JSONObject;
import net.wanho.commons.exception.ServiceException;
import net.wanho.commons.factory.BeanFactory;
import net.wanho.commons.servlet.BaseServlet;
import net.wanho.commons.vo.AjaxResult;
import net.wanho.systemSettings.po.Employee;
import net.wanho.systemSettings.po.Menu;
import net.wanho.systemSettings.service.EmployeeService;
import net.wanho.systemSettings.service.MenuService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/LoginServlet")
public class LoginServlet extends BaseServlet<EmployeeService> {

   MenuService menuService = BeanFactory.getBean(MenuService.class);
	
	protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		String methodName = request.getParameter("method");
		try {
			Method method = getClass().getDeclaredMethod(methodName,
					HttpServletRequest.class, HttpServletResponse.class);
			method.setAccessible(true);
			method.invoke(this, request, response);
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}
	
	protected AjaxResult loginCheck(HttpServletRequest request, HttpServletResponse response) throws ServletException {
        try {
            String employeeId = request.getParameter("employeeId");
            String password = request.getParameter("password");

            Employee employee = super.baseService.findById(employeeId);
            if(employee==null || !employee.getLogIn().getEmmPassword().equals(password))
            {
               return AjaxResult.fail("工号或密码错误！");
            }
            //状态锁定
            if(employee.getStatus().equals("0"))
            {
               return  AjaxResult.fail("账号密码锁定！");
            }
            //是否关联职位
            if(employee.getPosition().getPositionId()==0)
            {
                return  AjaxResult.fail("您没有权限访问任何资源");
            }
            request.getSession().setAttribute("currEmployee",employee);
            return AjaxResult.success() ;
        } catch (ServiceException e) {
            e.printStackTrace();
            return AjaxResult.fail("登录异常") ;
        }
    }
	
	protected String loginToCms(HttpServletRequest request, HttpServletResponse response) throws ServletException {
		try {
		/*	System.out.println("toIndex页面");*/
            HttpSession session = request.getSession();
            Employee employee = (Employee) session.getAttribute("currEmployee");
            int positionid = employee.getPosition().getPositionId();
            Map<String,String> condMap = new HashMap<>(1);
            condMap.put("positionId",String.valueOf(positionid));
            //获得当前职位关联的菜单集合
            List<Menu> menuList = menuService.findByCond(condMap);
            //当前员工的职位所关联的菜单集合
            employee.getPosition().setMenuList(menuList);
            return "redirect:/ControlServlet?method=countAll" ;
			//response.sendRedirect(request.getContextPath()+ "/ControlServlet?method=countAll");
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServletException(e.getMessage());
		}
    }
	protected void updatePassword(HttpServletRequest request,HttpServletResponse response){
		JSONObject json = new JSONObject();
			json.put("status", "1");
			json.put("msg", "修改成功");
		try {
			response.getWriter().write(json.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	protected void logout(HttpServletRequest request,HttpServletResponse response){
		try {
			response.sendRedirect(request.getContextPath()+ "/login.jsp");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
